package com.competition.commgr.service.impl;

import java.util.List;
import com.competition.common.utils.DateUtils;
import com.competition.commgr.constants.ReviewStepConstants; // 新增导入
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.competition.commgr.mapper.RegistrationReviewMapper;
import com.competition.commgr.domain.RegistrationReview;
import com.competition.commgr.service.IRegistrationReviewService;

/**
 * 报名审核Service业务层处理
 */
@Service
public class RegistrationReviewServiceImpl implements IRegistrationReviewService
{
    @Autowired
    private RegistrationReviewMapper registrationReviewMapper;

    /**
     * 查询报名审核
     */
    @Override
    public RegistrationReview selectRegistrationReviewByReviewId(Long reviewId)
    {
        RegistrationReview review = registrationReviewMapper.selectRegistrationReviewByReviewId(reviewId);
        // 设置显示名称（如果需要在前端显示）
        if (review != null && review.getStep() != null) {
            review.setStep(ReviewStepConstants.getDisplayName(review.getStep()));
        }
        return review;
    }

    /**
     * 查询报名审核列表
     */
    @Override
    public List<RegistrationReview> selectRegistrationReviewList(RegistrationReview registrationReview)
    {
        // 如果查询条件中有步骤，也需要规范化
        if (registrationReview != null && registrationReview.getStep() != null) {
            String normalizedStep = ReviewStepConstants.normalizeStep(registrationReview.getStep());
            registrationReview.setStep(normalizedStep);
        }

        List<RegistrationReview> list = registrationReviewMapper.selectRegistrationReviewList(registrationReview);

        // 为返回结果设置显示名称
        for (RegistrationReview review : list) {
            if (review.getStep() != null) {
                review.setStep(ReviewStepConstants.getDisplayName(review.getStep()));
            }
        }
        return list;
    }

    /**
     * 新增报名审核
     */
    @Override
    public int insertRegistrationReview(RegistrationReview registrationReview)
    {
        // 统一步骤名称处理
        if (registrationReview.getStep() != null) {
            String normalizedStep = ReviewStepConstants.normalizeStep(registrationReview.getStep());
            registrationReview.setStep(normalizedStep);
        }

        // 设置创建时间（如果需要）
        // registrationReview.setCreateTime(DateUtils.getNowDate());
        return registrationReviewMapper.insertRegistrationReview(registrationReview);
    }

    /**
     * 修改报名审核
     */
    @Override
    public int updateRegistrationReview(RegistrationReview registrationReview)
    {
        // 统一步骤名称处理
        if (registrationReview.getStep() != null) {
            String normalizedStep = ReviewStepConstants.normalizeStep(registrationReview.getStep());
            registrationReview.setStep(normalizedStep);
        }
        return registrationReviewMapper.updateRegistrationReview(registrationReview);
    }

    /**
     * 批量删除报名审核
     */
    @Override
    public int deleteRegistrationReviewByReviewIds(Long[] reviewIds)
    {
        return registrationReviewMapper.deleteRegistrationReviewByReviewIds(reviewIds);
    }

    /**
     * 删除报名审核信息
     */
    @Override
    public int deleteRegistrationReviewByReviewId(Long reviewId)
    {
        return registrationReviewMapper.deleteRegistrationReviewByReviewId(reviewId);
    }

    /**
     * 根据报名ID和步骤查询审核记录（新增方法）
     */
    public RegistrationReview selectByRegIdAndStep(Long regId, String step) {
        RegistrationReview query = new RegistrationReview();
        query.setRegId(regId);
        query.setStep(ReviewStepConstants.normalizeStep(step)); // 规范化步骤

        List<RegistrationReview> list = registrationReviewMapper.selectRegistrationReviewList(query);
        return list.isEmpty() ? null : list.get(0);
    }
}